package com.example.ruihuada.widget

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.annotation.ColorInt
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import com.example.ruihuada.R
import com.example.util.gone
import com.example.util.visible

class ToolBarBuild constructor(val context: AppCompatActivity) {
    var title: String? = null

    @ColorInt
    var titleTextColor: Int? = null

    @ColorInt
    var toolBarBackgroundColor: Int? = null
    var titleVisible: Boolean = true
    var backIcon: Int = 0
    var backVisible: Boolean = true
    var titleTv: TextView? = null

    fun build(parent: ViewGroup): ToolBarBuild {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.base_toolbar, null)
        val toolBarContain = view.findViewById<RelativeLayout>(R.id.toolBarContain)
        titleTv = view.findViewById<TextView>(R.id.titleTv)
        val backLayout = view.findViewById<ImageView>(R.id.backIv)
        toolBarContain.setBackgroundColor(toolBarBackgroundColor ?: Color.WHITE)
        backLayout.setOnClickListener { context.finish() }
        titleTv?.text = title
        titleTv?.setTextColor(titleTextColor ?: ContextCompat.getColor(context, R.color.colorBlack))

        if (!titleVisible) {
            titleTv?.gone()
        }
        parent.removeAllViews()
        parent.addView(view)
        return this
    }
}